package com.bw.zg3_day05zy.news

import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.bw.base.entity.NewsBeanItem
import com.bw.base.entity.NewsDetailBean
import com.bw.base.mvvm.BaseViewModel
import com.bw.base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class NewsViewModel @Inject constructor(val apiService: ApiService): BaseViewModel() {
    fun getNews():Flow<PagingData<NewsBeanItem>>{
        return Pager(config = PagingConfig(pageSize = 10), pagingSourceFactory = {
            NewsDataSource(apiService)
        }).flow.cachedIn(viewModelScope)
    }

    val detailData = MutableLiveData<NewsDetailBean>()
    fun newsDetail(id: Int){
        viewModelScope.launch {
            httpRequest({apiService.newsDetail(id)},detailData::postValue)
        }
    }
}